Ext.ns('journal.home');

journal.home.EntryForm = Ext.extend(Ext.form.FormPanel, (function() {

    var logger = journal.LoggerFactory.getLogger('journal.home.EntryForm');
    return {
        // defaults - can be changed from outside
        border : false,
        frame : true,
        labelWidth : 80,
        constructor : function(config) {
            config = config || {};
            config.listeners = config.listeners || {};
            journal.home.EntryForm.superclass.constructor.call(this, config);
        },

        /** Disable this form. Issue to address is cursor loss after setting read only state. */
        setDisabled : function(bool) {
            this.get(4).setReadOnly(bool);
        },

        initComponent : function() {

            // hard coded - cannot be changed from outside
            var config = {
                id : 'codeForm_' + this.initialConfig.key,
                url : 'home?action=saveEntry',
                frame : true,
                /**
                 * Monitors length of html. Will trigger dirty flag if this changes on htmleditor toolbar click.
                 */
                htmlCount : null,
                undoCount : 0,
                autoHeight : true,
                autoWidth : true,
                waitTitle : 'Saving data',
                waitMsg : 'Please wait...',
                items : [
                        {
                            xtype : 'hidden',
                            name : 'tabindex',
                            value : 't-' + this.initialConfig.key
                        },
                        {
                            xtype : 'hidden',
                            name : 'entryId'
                        },
                        {
                            xtype : 'hidden',
                            name : 'branchId',
                            value : !this.initialConfig.node.isLeaf() ? this.initialConfig.node.id : null
                        },
                        {
                            xtype : 'hidden',
                            name : 'title'
                        },
                        {
                            // xtype : 'htmleditor',
                            xtype : 'journal.ui.HtmlEditor',
                            id : 'word-textarea' + this.initialConfig.key,
                            name : 'detail',
                            enableKeyEvents : true,
                            hideLabel : true,
                            anchor : '100%',
                            cls : 'htmleditor',
                            height : 'auto',
                            width : '100%',
                            plugins : [ new Ext.ux.form.HtmlEditor.IndentOutdent(),
                                    new Ext.ux.form.HtmlEditor.Divider(), new Ext.ux.form.HtmlEditor.HR() ],
                            listeners : {
                                initialize : function(he) {
                                    this.he = he;
                                    Ext.EventManager.on(he.iframe.contentDocument, 'keypress', function(e) {
                                        journal.home.MainHandler.keyHandler(e, journal.home.Main);
                                        this.undoCount = journal.home.EntryEditorHandler.editorKeyHandler(e,
                                                this.he.el.dom, this.ownerCt.ownerCt, this.undoCount);
                                    }.createDelegate(this));

                                    Ext.EventManager.on(he.iframe.contentDocument, 'keypress', function() {
                                        if (!this.ownerCt
                                                || !this.ownerCt.ownerCt.tabFlag[this.getForm().findField('tabindex')
                                                        .getValue()]) {
                                            return;
                                        }
                                        var sb = Ext.getCmp('word-status');
                                        sb.showBusy('Saving draft...');
                                        (function() {
                                            sb.setStatus({
                                                iconCls : 'x-status-saved',
                                                text : 'Draft auto-saved at ' + new Date().format('g:i:s A')
                                            });
                                        }).defer(4000);
                                    }.createDelegate(this), this, {
                                        buffer : 5000
                                    // idle of 5 seconds will trigger auto save
                                    });

                                    //Will fix the issue with cursor resetting to 0,0 on click.
                                    he.updateToolbar();
                                }.createDelegate(this),

                                sync : function(he, html) {
                                    //var tabId = he.ownerCt.ownerCt.node.id;
                                    if (this.htmlCount != null && html.length > 0) {
                                        if (this.htmlCount != html.length) {
                                            //logger.debug('changed [' + this.htmlCount + '] to [' + html.length + ']');
                                            this.htmlCount = html.length;
                                            journal.home.EntryEditorHandler.dirtyFlagToggler(this.ownerCt.ownerCt);
                                        }
                                    } else if (html.length > 0) {
                                        this.htmlCount = html.length;
                                    }
                                }.createDelegate(this)

                            }
                        } ]
            }; // eo config object

            // apply config
            Ext.apply(this, Ext.apply(this.initialConfig, config));

            // call parent
            journal.home.EntryForm.superclass.initComponent.apply(this, arguments);

        }, // eo function initComponent

        /** Form override */
        onRender : function() {
            // call parent
            journal.home.EntryForm.superclass.onRender.apply(this, arguments);
            // set wait message target
            this.getForm().waitMsgTarget = this.getEl();
        } // eo function onRender

    };
})()); // eo extend

// register xtype
Ext.reg('journal.home.EntryForm', journal.home.EntryForm);